//
//  SecondPageCollectionViewCell.m
//  IT爱好者
//
//  Created by dllo on 15/10/15.
//  Copyright © 2015年 Fan. All rights reserved.
//

#import "SecondPageCollectionViewCell.h"
#import "CFNetworkFromAFNetworking.h"
#import "SectionListModel.h"
#import "RowListModel.h"
#import "SectionView.h"
#import "Header.h"
#import "RowTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "ForumViewController.h"

@implementation SecondPageCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _flag = 100000;
#pragma mark 菊花
        _HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
        _HUD.backgroundColor = [UIColor clearColor];
        _HUD.labelText = @"正在刷新";
        _HUD.detailsLabelText = @"请耐心等待!";
        
        [self getdata];
        [self creatView];
    }
    return self;
}

- (void)creatView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.contentView.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.contentView addSubview:_tableView];
}

-(void)buttonAction
{
    [_tableView reloadData];
}


#pragma mark 关于tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSourceArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80 * FitHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    SectionView *sectionView = [[SectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80 * FitHeight)];
    sectionView.SLModel = _dataSourceArray[section];
    sectionView.tag = section + 1000;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [sectionView addGestureRecognizer:tap];
#pragma mark 让sectionview 背景色交替
    if (section % 2 != 0) {
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"nightIsOn"]) {
            sectionView.backgroundColor = [UIColor grayColor];
        }else{
            sectionView.backgroundColor = [UIColor whiteColor];
        }
    }
    return sectionView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#pragma mark 判断哪个被点了 就返回数据 否则不给数据
    if (_flag == section) {
        return [[_dataSourceArray[section] list] count];
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier1 = @"cell1";
    RowTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1];
    if (!cell1) {
        cell1 = [[RowTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
    }
    cell1.RLModel = [[_dataSourceArray[indexPath.section] list] objectAtIndex:indexPath.row];
    return cell1;
}

#pragma mark 当被点击时 代理调用推到下一页 并传一个RowListModel 的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate pushAndpassValue:[[_dataSourceArray[indexPath.section] list] objectAtIndex:indexPath.row]];
    
}

#pragma mark 点击section纪录哪个被点了
- (void)tapAction:(UITapGestureRecognizer *)tap;
{
    SectionView *view = (SectionView *)tap.view;
    if (_flag != view.tag - 1000) {
        _flag = 100000;
        [_tableView reloadData];
        _flag = view.tag - 1000;

    }else{
        _flag = 100000;
    }
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:(view.tag - 1000)];
    [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

#pragma mark 解析数据
- (void)getdata
{
    self.dataSourceArray = [NSMutableArray array];
    NSString *urlString = @"http://lib.wap.zol.com.cn/bbs/ios/board.php?isReviewing=NO&vs=430";
    [CFNetworkFromAFNetworking askObjectByURLString:urlString BodyString:nil WithObjectBlock:^(id object) {
        NSArray *array = [object objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            //建一个sectionListModel 装界面显示信息
            SectionListModel *SLM = [[SectionListModel alloc]init];
            [SLM setValuesForKeysWithDictionary:dic];
            SLM.list = [NSMutableArray array];
            NSArray *arr = [dic objectForKey:@"list"];
            for (NSDictionary *dict in arr) {
                //建一个rowListModel 装cell显示的信息
                RowListModel *RLM = [[RowListModel alloc]init];
                [RLM setValuesForKeysWithDictionary:dict];
                [SLM.list addObject:RLM];
            }
            [self.dataSourceArray addObject:SLM];
        }
        [_tableView reloadData];
        _HUD.hidden = YES;
    }];
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
